<?php

namespace app\api\controller;

use think\Controller;
use think\Request;
use app\api\model\SupplierModel;
use app\api\validate\SupplierValidate;

/**
 * 供应商
 * Class Supplier
 * @package app\api\controller
 */
class Supplier extends Common
{
    /**
     * @param Request $request
     * @param SupplierModel $model
     * @return \think\response\Json
     */
    public function index(Request $request, SupplierModel $model)
    {
        $list = $model->getList($request->param());
        return $this->returnJson(200, '成功', $list);
    }

    public function getAll(SupplierModel $model)
    {
        $list =$model->getAll();
        return $this->returnJson(200, '成功', $list);
    }

    /**
     * @param Request $request
     * @param SupplierModel $model
     * @return \think\response\Json
     */
    public function add(Request $request, SupplierModel $model)
    {
        $data = $request->param();
        $data['create_time'] = time();//添加时间
        $validate = new SupplierValidate();
        if (!$validate->check($data)) {
            return $this->returnJson(401, $validate->getError());
        }
        $arr = $model->getAdd($data);
        if (!empty($arr)) {
            return $this->returnJson(200, '添加成功', $arr);
        } else {
            return $this->returnJson(500, '添加失败');
        }
    }

    /**
     * @param $id
     * @param SupplierModel $model
     * @return \think\response\Json
     */
    public function show($id, SupplierModel $model)
    {
        $arr = $model->getInfo($id);
        if (!empty($arr)) {
            return $this->returnJson(200, '成功', $arr);
        } else {
            return $this->returnJson(404, '失败');
        }
    }

    /**
     * @param $id
     * @param Request $request
     * @param SupplierModel $model
     * @return \think\response\Json
     */
    public function edit($id, Request $request, SupplierModel $model)
    {
        $data = $request->param();
        $arr = $model->getSupplierEdit($id, $data);
        if (!empty($arr)) {
            return $this->returnJson(200, '成功', $arr);
        } else {
            return $this->returnJson(404, '失败');
        }
    }

    /**
     * 供应商删除
     * @param int $id 供应商id
     * @param SupplierModel $model
     * @return Json
     */
    public function del($id, SupplierModel $model)
    {
        $arr = $model->getSupplierDel($id);
        if ($arr) {
            return $this->returnJson(500, '欠供应商款,无法删除!');
        } else {
            return $this->returnJson(200, '删除成功');
        }
    }
}
